package com.caiheng.api.service.platfom.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.EstateMapper;
import com.caiheng.api.dao.estate.PropertyMapper;
import com.caiheng.api.dao.platform.PlatformPayMapper;
import com.caiheng.api.entity.estate.Estate;
import com.caiheng.api.entity.estate.Property;
import com.caiheng.api.entity.platform.PlatformPay;
import com.caiheng.api.pay.nongshang.NSAppletOrders;
import com.caiheng.api.pay.nongshang.NSQuery;
import com.caiheng.api.pay.nongshang.NSRefund;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.platfom.PlatformPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 银行支付列Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@Service
public class PlatformPayServiceImpl extends BaseServiceImpl<PlatformPayMapper, PlatformPay> implements PlatformPayService {

    @Resource
    private PlatformPayMapper platformPayMapper;
    @Resource
    private PropertyMapper propertyMapper;
    @Resource
    private EstateMapper estateMapper;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Value("${web.appid}")
    private String appid;

    @Value("${web.nsh_notify_url}")
    private String nsh_notify_url;


    /**
     *  农商行的  微信下单
     * @param out_trade_no
     * @param totalAmount
     * @param subject
     * @param open_id
     * @param attach
     * @param platformPay
     * @return
     */
    @Override
    public String appletOrderMain(String out_trade_no, BigDecimal totalAmount, String subject, String open_id, String attach,String notifyUrl, PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSAppletOrders.addOrderMain(out_trade_no,platformPay,appid,open_id,totalAmount,attach,notifyUrl,subject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    /**
     *  农商行的  微信下单
     * @param out_trade_no
     * @param totalAmount
     * @param subject
     * @param open_id
     * @param attach
     * @param platformPay
     * @return
     */
    @Override
    public String appletOrder(String out_trade_no, BigDecimal totalAmount, String subject, String open_id, String attach,String notifyUrl, PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSAppletOrders.addOrder(out_trade_no,platformPay,appid,open_id,totalAmount,attach,notifyUrl,subject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    /**
     *  农商行的  微信下单
     * @param out_trade_no
     * @param totalAmount
     * @param subject
     * @param open_id
     * @param attach
     * @param platformPay
     * @return
     */
    @Override
    public String appletOrderUnCart(String out_trade_no, BigDecimal totalAmount, String subject, String open_id, String attach,String notifyUrl, PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSAppletOrders.addOrderUnCart(out_trade_no,platformPay,appid,open_id,totalAmount,attach,notifyUrl,subject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }

    @Override
    public String orderQuery(String out_trade_no, PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSQuery.queryOrder(out_trade_no,platformPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }

    /**
     * 退款
     * @param out_trade_no
     * @param refund_money
     * @param out_refund_no
     * @param platformPay
     * @return
     */
    @Override
    public String refundOrder(String out_trade_no, BigDecimal refund_money, String out_refund_no,PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSRefund.refundOrder(out_trade_no,refund_money,out_refund_no,platformPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }

    @Override
    public String refundOrderQuery(String out_trade_no, PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSQuery.queryOrder(out_trade_no,platformPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    @Override
    public PlatformPay getDefaultAccount() {
        QueryWrapper<PlatformPay> qw = new QueryWrapper<>();
        qw.eq("is_default",1);
        qw.last("limit 1");
        PlatformPay platformPay = null;

        String key = "platform_pay_default";
        boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
        if (blHasKey) {
            Object object = redisTemplate.opsForHash().get(key, key);
            String itemDicts = object != null ? object.toString() : "";
            if (StrUtil.isNotEmpty(itemDicts)) {
                JSONObject jsonObject = JSONUtil.parseObj(itemDicts);
                platformPay = JSONUtil.toBean(JSONUtil.toJsonStr(jsonObject), PlatformPay.class);
            } else {
                platformPay = platformPayMapper.selectOne(qw);
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(platformPay));
            }
        } else {
            platformPay = platformPayMapper.selectOne(qw);
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(platformPay));
        }
        return platformPay;
    }

    @Override
    public PlatformPay getAccountByPropertyId(int propertyId) {
        PlatformPay platformPay = null;
        Property property = propertyMapper.selectById(propertyId);
        if(property!=null){
            String key = "platform_pay_property_" + property.getPayPlatformId();
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONObject jsonObject = JSONUtil.parseObj(itemDicts);
                    platformPay = JSONUtil.toBean(JSONUtil.toJsonStr(jsonObject), PlatformPay.class);
                } else {
                    platformPay = platformPayMapper.selectById(property.getPayPlatformId());
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(platformPay));
                }
            } else {
                platformPay = platformPayMapper.selectById(property.getPayPlatformId());
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(platformPay));
            }
        }
        return platformPay;
    }


    @Override
    public PlatformPay getAccountByEstateId(int estateId) {
        PlatformPay platformPay = null;
        Estate estate = estateMapper.selectById(estateId);
        if(estate!=null){
            String key = "platform_pay_estate_" + estate.getPayPlatformId();
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONObject jsonObject = JSONUtil.parseObj(itemDicts);
                    platformPay = JSONUtil.toBean(JSONUtil.toJsonStr(jsonObject), PlatformPay.class);
                } else {
                    platformPay = platformPayMapper.selectById(estate.getPayPlatformId());
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(platformPay));
                }
            } else {
                platformPay = platformPayMapper.selectById(estate.getPayPlatformId());
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(platformPay));
            }
        }
        return platformPay;
    }
}
